/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package apteka;

import apteka.tables.Category;
import apteka.tables.Product;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Marek
 */
@Component
public class CategoryManager {


    Session session=null;

    public CategoryManager() {
        session=HibernateUtil.getSession();
    }

    public List<Category> getMainCategories() {
        List<Category> result = null;
        try {
            Transaction trans = session.beginTransaction();
            Query q = session.createQuery("FROM Category AS category WHERE  category.category.idcategory=0 ORDER BY category.categoryname");
            result = (List<Category>)q.list();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return result;
    }

    public List<Category> getSubCategories(int id_main) {
        List<Category> result = null;
        try {
            Transaction trans = session.beginTransaction();
            Query q = session.createQuery("FROM Category AS category WHERE category.category.idcategory="+id_main+" ORDER BY category.categoryname");
            result = (List<Category>)q.list();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return result;
    }

    public List<Category> getCategories() {
        List<Category> result = null;
        try {
            Transaction trans = session.beginTransaction();
            Query q = session.createQuery("FROM Category AS category WHERE category.idcategory!=0 ORDER BY category.categoryname");
            result = (List<Category>)q.list();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return result;
    }

    public Category getCategory(int id) {
        Category result = null;
        try {
            Transaction trans = session.beginTransaction();
            Query q = session.createQuery("FROM Category AS category WHERE category.idcategory="+id);
            result = (Category)q.uniqueResult();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return result;
    }

    public void deleteCategory(int id) {
        if (id!=0) {
            Transaction trans=null;
            try {
                Category cat = getCategory(id);
                trans = session.beginTransaction();
                trans.begin();
                Query q = session.createQuery("FROM Category AS category WHERE category.category.idcategory="+id);
                List<Category> cats = q.list();
                    for (int a=0;a<cats.size();a++) {
                        cats.get(a).setCategory(cat.getCategory());
                        session.update(cats.get(a));
                    }
                session.delete(cat);
                session.flush();
                trans.commit();

            } catch (Exception e) {
                 System.out.println(e.toString());
                 trans.rollback();
            }
        }
    }

    public void addCategory(String name) {
        Transaction trans = null;
        try {
            trans = session.beginTransaction();
            trans.begin();
            Category n_cat = new Category();
            n_cat.setCategoryname(name);
            n_cat.setCategory(getCategory(0));
            session.save(n_cat);
            session.flush();
            trans.commit();
        } catch (Exception e) {
            System.out.println(e.toString());
            trans.rollback();
        }
    }

    public void addCategory(int parent,String name) {
        Transaction trans = null;
        try {
            trans = session.beginTransaction();
            trans.begin();
            Category n_cat = new Category();
            n_cat.setCategoryname(name);
            n_cat.setCategory(getCategory(parent));
            session.save(n_cat);
            session.flush();
            trans.commit();
        } catch (Exception e) {
            System.out.println(e.toString());
            trans.rollback();
        }
    }

    public void editCategory(int id, String name) {
        Transaction trans = session.beginTransaction();;
        try {
            Category n_cat = getCategory(id);
            n_cat.setCategoryname(name);
            session.saveOrUpdate(n_cat);
            session.flush();
            session.refresh(n_cat);
            trans.commit();
        } catch (Exception e) {
            System.out.println(e.toString());
            trans.rollback();
        }
    }

    public void moveCategory(int id,int to) {
        Transaction trans = session.beginTransaction();
        try {
            Category n_cat = getCategory(id);
            n_cat.setCategory(getCategory(to));
            session.update(n_cat);
            session.flush();
            session.refresh(n_cat);
            trans.commit();
        } catch (Exception e) {
            System.out.println(e.toString());
            trans.rollback();
        }
    }
  
}
